package com.dunke.web.config;

import com.dunke.web.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * 拦截器
 */
@Configuration
public class LoginConfig implements WebMvcConfigurer {

    @Bean
    public HandlerInterceptor loginInterceptor(){
        return new LoginInterceptor();
    }
    /*
     * 静态资源映射
     * */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/") ;
    }
    @Override
    public void addInterceptors (InterceptorRegistry registry){
        // 排除拦截
        List<String> excludePath = new ArrayList<>();
        excludePath.add("/user/login");  // 测试登录
        excludePath.add("/admin/login");  // 管理员登录
        excludePath.add("/admin/register");  // 管理员注册
        excludePath.add("/admin/forgotPwd");  // 管理员忘记密码
        excludePath.add("/user/log");
        excludePath.add("/user/login.html");
        System.out.println(excludePath.size());
        // 绑定登录拦截器
        registry.addInterceptor(loginInterceptor())
                //表示拦截所有请求
                .addPathPatterns("/**")
                //表示取消对特定路径的拦截
                .excludePathPatterns(excludePath);
//        .excludePathPatterns("/admin/login");
        //这里一定不要写成/**/*.js的形式，spring boot无法识别
        //取消对static目录下静态资源的拦截
        // .excludePathPatterns("/static/**") ;

//        WebMvcConfigurer.super.addInterceptors(registry);


    }

}
